package com.fmsz.xmly.fragments.findfgts;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.anchor.AnchorAdapter;
import com.fmsz.xmly.entitys.anchor.AnchorEntity;
import com.fmsz.xmly.utils.Urls;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;


public class FindAnchorFragment extends Fragment {
    private RecyclerView recyclerView;
    private List<AnchorEntity> list = new ArrayList<>();
    private AnchorAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_anchor, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.anchor_recycler);
       recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        getData();
        adapter = new AnchorAdapter(list, getContext());
        recyclerView.setAdapter(adapter);

    }

    //网络请求数据
    private void getData() {
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, Urls.ANCHOR, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray array = new JSONObject(response).getJSONArray("list");
                    List<AnchorEntity> anchorEntities = JSON.parseArray(array.toString(), AnchorEntity.class);
                    list.addAll(anchorEntities);
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }, null);

        requestQueue.add(stringRequest);

    }


}



























